<template>
  <div class="analysis eig-column">
    <div class="eig-filter" style="padding: 20px 20px 10px">
      <div class="filter-left">
        <a-select
          v-model:value="stateValue"
          style="width: 140px"
          placeholder="State"
          @change="changeMode"
          :disabled="configData.hrv.isAnalysis"
          :options="channelOptions"
        ></a-select>
      </div>
      <div class="filter-right">
        <!-- <a-button type="primary" @click="reportRecordSucess"
          >reportRecordSucess</a-button
        > -->
        <!-- <a-button type="primary" @click="calculate">calculate</a-button> -->
        <a-button
          v-if="!configData.hrv.isAnalysis"
          type="primary"
          @click="changeStatus"
          >Start</a-button
        >
        <a-button danger type="primary" v-else @click="changeStatus"
          >Stop</a-button
        >
      </div>
    </div>
    <div class="eig-flex analysis-container">
      <video
        ref="videoRef"
        style="
          width: 100%;
          height: 100%;
          background-color: #000;
          object-fit: fill;
        "
        :src="videoSrc"
      ></video>
    </div>
    <div class="eig-shrink analysis-footer" style="height: 250px">
      <div>
        <p class="card-title">{{ stateValue }}</p>
        <lineChart
          id="lineChart1"
          ref="lineChart1"
          :numSeconds="numSeconds"
          :channel="concentrationChannel"
          :colors="concentrationColors"
          :isShowName="false"
          :bothGridLines="true"
          :yMax="1"
          :autoDraw="true"
          :yMin="0"
          style="width: 100%; height: 100%"
        ></lineChart>
      </div>
      <div>
        <p class="card-title">HeartRate</p>
        <lineChart
          id="lineChart2"
          ref="lineChart2"
          :channel="heartRateChannel"
          :numSeconds="numSeconds"
          :isAutoScaleY="true"
          :autoDraw="true"
          :bothGridLines="true"
          :isShowName="false"
          :colors="heartRateColors"
          :yMax="120"
          :yMin="0"
          style="width: 100%; height: 100%"
        ></lineChart>
      </div>
    </div>
    <a-modal
      v-model:open="openStartRecordModal"
      :title="'创建报告记录'"
      cancelText="取消"
      :maskClosable="false"
      okText="确认"
      @ok="handleStartRecordModal"
      @cancel="handleEndRecordModal"
    >
      <a-form
        style="padding-top: 30px"
        name="basic"
        :label-col="{ span: 4 }"
        :wrapper-col="{ span: 18 }"
        autocomplete="off"
      >
        <a-form-item
          label="报告名称"
          name="reportName"
          v-bind="validateInfos.reportName"
        >
          <a-input v-model:value="formData.reportName" />
        </a-form-item>
        <a-form-item label="姓名" name="name" v-bind="validateInfos.name">
          <a-input v-model:value="formData.name" />
        </a-form-item>
        <a-form-item label="年龄" name="age" v-bind="validateInfos.age">
          <a-input-number
            style="width: 40%"
            v-model:value="formData.age"
            :min="0"
            :max="200"
            addon-after="岁"
          />
        </a-form-item>
        <a-form-item label="性别" name="gender" v-bind="validateInfos.gender">
          <a-radio-group v-model:value="formData.gender">
            <a-radio :value="1">男</a-radio>
            <a-radio :value="0">女</a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </a-modal>
    <a-modal
      :maskClosable="false"
      v-model:open="openExportRecord"
      :title="'心灵之旅报告'"
      cancelText="取消"
      width="1200px"
      okText="导出PDF"
      @ok="handleExportRecord"
      @cancel="cancelExportRecord"
    >
      <div class="pdf-page-box-model">
        <div class="pdf-page-box" ref="pdf">
          <div class="pdf-page">
            <p class="pdf-title">BioMulti EEG-FNIRS Lite 心灵之旅报告</p>
            <p class="pdf-subtitle">基础信息</p>
            <ul class="pdf-base-info">
              <li>
                <span>姓名：</span>
                <span>{{ formData.name }}</span>
              </li>
              <li>
                <span>年龄：</span>
                <span>{{ formData.age }}</span>
              </li>
              <li>
                <span>性别：</span>
                <span>{{ formData.gender == 1 ? "男" : "女" }}</span>
              </li>
              <li>
                <span>检查时间：</span>
                <span>{{
                  formatTimestamp(
                    formData.recoredCreateTime,
                    "yyyy-MM-dd",
                    true
                  )
                }}</span>
              </li>
              <li>
                <span>检查号：</span>
                <span>{{ recordLastID }}</span>
              </li>
            </ul>
            <div class="pdf-line"></div>
            <p class="pdf-subtitle">脑电时域指标</p>
            <div
              class="pdf-section"
              v-for="(item, index) in eegChannel"
              :key="index"
            >
              <div class="pdf-label">{{ item }}</div>
              <p class="pdf-section-title">时域波形</p>
              <div
                class="pdf-chart-box"
                :style="{ height: eegTimeHeight[item] + 'px' }"
              >
                <lineChart
                  :id="`eegTime_${index}`"
                  ref="eegTimeRefs"
                  :channel="[item]"
                  :numSeconds="numSeconds"
                  :isAutoScaleY="true"
                  :isShowName="true"
                  :colors="eegTimeSeriesColors"
                  :yMax="1"
                  :scale="0.8"
                  :pixelRatio="2"
                  :autoDraw="false"
                  :yMin="0"
                  style="width: 100%; height: 100%"
                ></lineChart>
              </div>
              <p class="pdf-section-title">节律波形</p>
              <div
                class="pdf-chart-box"
                :style="{ height: eegBandsHeight[item] + 'px' }"
              >
                <lineChart
                  :id="`eegBandsTime_${index}`"
                  ref="eegBandsTimeRefs"
                  :channel="eegBandsTimeChannel"
                  :numSeconds="numSeconds"
                  :isAutoScaleY="true"
                  :colors="bandsTimeColors"
                  :yMax="1"
                  :scale="0.8"
                  :grid="{
                    left: 70,
                    right: 10,
                    middle: 10,
                    top: 10,
                    bottom: 30,
                  }"
                  :pixelRatio="2"
                  :autoDraw="false"
                  :yMin="0"
                  style="width: 100%; height: 100%"
                ></lineChart>
              </div>
            </div>
          </div>
          <div class="pdf-page">
            <div class="pdf-line" style="margin-top: 0"></div>
            <p class="pdf-subtitle">脑电频域指标</p>
            <div
              class="pdf-section"
              v-for="(item, index) in eegChannel"
              :key="index"
              :style="{
                paddingBottom: index === eegChannel.length - 1 ? '0px' : '15px',
              }"
            >
              <div class="pdf-label">{{ item }}</div>
              <!-- <p class="pdf-section-title">脑电功率谱</p>
              <div class="pdf-chart-box" style="height: 210px">
                <lineChart
                  id="eegPsdFp1Chart"
                  ref="eegPsdFp1Chart"
                  :channel="eegPsdFp1Channel"
                  :numSeconds="eegPsdMaxFreq"
                  :isAutoScaleY="false"
                  :isShowName="true"
                  :bothGridLines="true"
                  :nameList="['dBμV^2']"
                  :colors="eegPsdColors"
                  :yMax="100"
                  :yMin="-60"
                  :scale="0.8"
                  :pixelRatio="2"
                  :autoDraw="false"
                  style="width: 100%; height: 100%"
                ></lineChart>
              </div> -->
              <p class="pdf-section-title">脑电节律能量</p>
              <div
                class="pdf-chart-box"
                :style="{ height: egRelatedPowerHeight[item] + 'px' }"
              >
                <lineChart
                  :id="`eegRelatedPower_${index}`"
                  ref="eegRelatedPowerRefs"
                  :channel="[item]"
                  :isShowName="true"
                  :nameList="['uV^2/Hz']"
                  :isMerge="true"
                  :setLogScale="true"
                  :showLegend="true"
                  :horizontalGridLines="true"
                  :seriesChannel="eegRelatedPowerChannel"
                  :numSeconds="numSeconds"
                  :isAutoScaleY="false"
                  :colors="relatedPowerColors"
                  :yMax="1000000"
                  :scale="0.8"
                  :grid="{
                    left: 70,
                    right: 10,
                    middle: 10,
                    top: 10,
                    bottom: 30,
                  }"
                  :pixelRatio="2"
                  :autoDraw="false"
                  :yMin="0.1"
                  style="width: 100%; height: 100%"
                ></lineChart>
              </div>
            </div>
            <div class="pdf-line" style="margin-top: 0"></div>
            <p class="pdf-subtitle">
              {{ stateValue == "concentration" ? "脑电专注力" : "放松度指标" }}
            </p>
            <div class="pdf-chart-box" style="height: 310px; margin-bottom: 0">
              <lineChart
                id="mindRestChart"
                ref="mindRestChart"
                :numSeconds="numSeconds"
                :channel="concentrationChannel"
                :colors="concentrationColors"
                :isShowName="true"
                :nameList="[stateValue]"
                :scale="0.8"
                :bothGridLines="true"
                :pixelRatio="2"
                :yMax="1"
                :autoDraw="false"
                :yMin="0"
                style="width: 100%; height: 100%"
              ></lineChart>
            </div>
          </div>
          <div class="pdf-page">
            <div class="pdf-line" style="margin-top: 0"></div>
            <p class="pdf-subtitle">近红外时域指标</p>
            <div class="pdf-chart-box" style="height: 600px">
              <lineChart
                id="irTimeSerie"
                ref="irTimeSerie"
                :isMerge="true"
                :seriesChannel="irTimeSerieChannel"
                :channel="irTimeChannel"
                :numSeconds="numSeconds"
                :colors="irTimeColors"
                :isAutoScaleY="true"
                :autoDraw="false"
                :isShowName="true"
                :grid="{
                  left: 70,
                  right: 10,
                  middle: 10,
                  top: 10,
                  bottom: 30,
                }"
                :scale="0.8"
                :pixelRatio="2"
                :yMax="1"
                :yMin="0"
                style="width: 100%; height: 100%"
              ></lineChart>
            </div>
            <div class="pdf-line" style="margin-top: 0"></div>
            <p class="pdf-subtitle">HRV指标</p>
            <p class="pdf-section-title">心率变化图</p>
            <div
              class="pdf-chart-box"
              :style="{
                height:
                  configData.eegFilter.eeg_channel_count == 4
                    ? '320px'
                    : '200px',
              }"
            >
              <lineChart
                id="HeartRateChart"
                ref="HeartRateChart"
                :numSeconds="numSeconds"
                :channel="concentrationChannel"
                :colors="concentrationColors"
                :isAutoScaleY="true"
                :isShowName="false"
                :bothGridLines="true"
                :scale="0.8"
                :pixelRatio="2"
                :yMax="120"
                :autoDraw="false"
                :yMin="0"
                style="width: 100%; height: 100%"
              ></lineChart>
            </div>
            <p class="pdf-section-title">时域指标</p>
            <a-table
              class="time-indicator-table"
              :columns="timeIndicatorTableColumns"
              :data-source="timeIndicatorTable"
              bordered
              :pagination="false"
            >
            </a-table>
          </div>
          <div
            class="pdf-page"
            :style="{
              height: 'auto',
              marginTop:
                configData.eegFilter.eeg_channel_count == 4 ? '15px' : '0px',
            }"
          >
            <p class="pdf-section-title">频域指标</p>
            <a-table
              class="time-indicator-table"
              :columns="frequencyIndicatorTableColumns"
              :data-source="frequencyIndicatorTable"
              bordered
              :pagination="false"
            >
            </a-table>
            <p class="pdf-section-title" style="margin-top: 15px">
              频域指标频带划分
            </p>
            <a-table
              style="margin-bottom: 20px"
              class="time-indicator-table"
              :columns="frequencyIndicatorBandTableColumns"
              :data-source="frequencyIndicatorBandTable"
              bordered
              :pagination="false"
            >
            </a-table>
          </div>
        </div>
      </div>
    </a-modal>
  </div>
</template>

<script setup lang="ts">
import {
  ref,
  reactive,
  onMounted,
  onBeforeUnmount,
  watch,
  toRaw,
  nextTick,
  getCurrentInstance,
} from "vue";
const stateValue = ref<string>("Concentration");
import lineChart from "../../../components/lineChart.vue";
import type { SelectProps } from "ant-design-vue";
import { CustomBluetooth } from "../../../utils/bluetooth";
import { formatTimestamp } from "../../../utils/common";
import { CustomDatabase } from "../../../utils/db";
import { useIndexStore } from "../../../store/index";
import { storeToRefs } from "pinia";
const ipcRenderer = require("electron").ipcRenderer;
import { message, Form, Modal } from "ant-design-vue";
import { DataItem } from "../../../types";
import relaxationVideo from "../../../assets/video/relaxation.mp4";
import concentrationVideo from "../../../assets/video/concentration.mp4";

const eegPsdMaxFreq = ref(45);
let pkgSourceData: any = [];
let hrvResult: any = {};

let db = new CustomDatabase();

let bluetooth = new CustomBluetooth();
const indexStore = useIndexStore();
import html2canvas from "html2canvas";
import jsPDF from "jspdf";
const {
  play,
  recordId,
  playIndex,
  isEegClear,
  isDragSlider,
  isConnect,
  playGap,
  configData,
} = storeToRefs(indexStore);
let eegPkgDataList: any = [];
let irPkgDataList: any = [];
const useForm = Form.useForm;
const eegChannelName = ["Fp1", "Fp2"];
const eegFourChannelName = ["Fpz", "AF7", "AF8", "M2"];
const eegTimeHeight = {
  Fp1: 170,
  Fp2: 170,
  Fpz: 170,
  AF7: 170,
  AF8: 210,
  M2: 210,
};
const eegBandsHeight = {
  Fp1: 380,
  Fp2: 380,
  Fpz: 380,
  AF7: 380,
  AF8: 420,
  M2: 420,
};
const egRelatedPowerHeight = {
  Fp1: 440,
  Fp2: 440,
  Fpz: 400,
  AF7: 400,
  AF8: 400,
  M2: 380,
};
const eegChannel = ref(eegChannelName);
const eegTimeRefs = ref<any>([]);
const eegBandsTimeRefs = ref<any>([]);
const eegRelatedPowerRefs = ref<any>([]);
const eegPsdFp1Chart = ref<any>(null);
const eegPsdFp2Chart = ref<any>(null);
// const eegRelatedPowerFp1Chart = ref<any>(null);
// const eegRelatedPowerFp2Chart = ref<any>(null);
const lineChart1 = ref<any>(null);
const lineChart2 = ref<any>(null);
const mindRestChart = ref<any>(null);
const HeartRateChart = ref<any>(null);
const irTimeSerie = ref<any>(null);
const videoRef = ref<any>(null);
const pdf = ref<any>(null);
const openStartRecordModal = ref<boolean>(false);
const openExportRecord = ref<boolean>(false);
let worker: Worker | null = null;
let workerInterval: Worker | null = null;
let fftpsResult: any = [];
let timer: any = null;
const numSeconds = ref(configData.value.hrv.total_time);
const restSeconds = ref(configData.value.hrv.rest_time);
let pkgMaxTime = numSeconds.value + 1;

interface FormState {
  recoredCreateTime: number;
  reportName: string;
  name: string;
  age: number;
  gender: 0 | 1; //0 女 1 男
}
const formData = reactive<FormState>({
  recoredCreateTime: 0,
  reportName: "",
  name: "",
  age: configData.value.irFilter.default_age,
  gender: 1,
});
const rulesRef = reactive({
  reportName: [{ required: true, message: "请输入报告名称" }],
  name: [{ required: true, message: "请输入姓名" }],
  age: [{ required: true, message: "请输入年龄" }],
  gender: [{ required: true, message: "请选择性别" }],
});
const { resetFields, validate, validateInfos } = useForm(formData, rulesRef);
const eegTimeSeriesFp1Channel = ref(["Fp1"]);
const eegSeriesTimeFp2Channel = ref(["Fp2"]);
const videoSrc = ref(concentrationVideo);
const app = getCurrentInstance();
const eegBandsTimeChannel = ref([
  "EEG",
  "DELTA",
  "THETA",
  "ALPHA",
  "BETA",
  "GAMMA",
]);
const eegRelatedPowerChannel = ref([
  "γ wave",
  "β wave",
  "α wave",
  "θ wave",
  "δ wave",
]);
const eegPsdFp1Channel = ref(["Fp1"]);
const eegPsdFp2Channel = ref(["Fp2"]);

const concentrationChannel = ref(["Fp1"]);
const heartRateChannel = ref(["Fp1"]);
const EEGTimeGap = 1000 / configData.value.eegFilter.sample_rate; // 采样间隔
let mindRestList: any = [];
let heartRateList: any = [];
const eegTimeSeriesColors = ref({
  Fp1: "#8FDCFE",
  Fp2: "#8FDCFE",
});

const eegPsdColors = ref({
  Fp1: "#8FDCFE",
  Fp2: "#8FDCFE",
});

const relatedPowerColors = ref({
  // "γ wave": "rgba(7, 33, 230, 0.8)",
  // "β wave": "rgba(76, 104, 255, 0.6)",
  // "α wave": "rgba(122, 131, 255, 0.6)",
  // "θ wave": "rgba(161, 156, 255, 0.6)",
  // "δ wave": "rgba(228, 205, 255, 0.6",
  "γ wave": "rgba(165, 107, 172, 255)",
  "β wave": "rgba(78, 123, 187, 255)",
  "α wave": "rgba(37, 146, 121, 255)",
  "θ wave": "rgba(241, 189, 0, 255)",
  "δ wave": "rgba(255, 67, 72, 255)",
});

const concentrationColors = ref({
  Fp1: "#8FDCFE",
});
const heartRateColors = ref({
  Fp1: "#8FDCFE",
});
const bandsTimeColors = ref({
  EEG: "#737373",
  DELTA: "#D5D5D6",
  THETA: "#A4A4FF",
  ALPHA: "#7BFFFF",
  BETA: "#FF72FF",
  GAMMA: "#E6E689",
});

const irTimeSerieChannel = ref(["HbO", "HbR", "HbT"]);
const irTimeChannel = ref([
  "S1D1",
  "S1D2",
  "S1D3",
  "S1D4",
  "S2D5",
  "S2D6",
  "S2D7",
  "S2D8",
]);

const irTimeColors = ref({
  HbO: "#ff0101",
  HbR: "#ffba01",
  HbT: "#0073ff",
});

const stateOptionsData = [
  {
    value: "Concentration",
    label: "Concentration",
  },
  {
    value: "Relaxation",
    label: "Relaxation",
  },
];
const channelOptions = ref<SelectProps["options"]>(stateOptionsData);
let recordLastID = ref("");
let tempRecordId = 0;

const handleExportRecord = () => {
  app?.proxy?.loading.show("导出中...");
  ipcRenderer.send("pdf-read", recordLastID.value);

  // exportPdf();
  // openExportRecord.value = false
};

const pdfReadResponse = (event, resp) => {
  if (resp.code == 201) {
    const url = URL.createObjectURL(
      new Blob([resp.data, { type: "application/pdf" }])
    );

    // Prompt the user to save the file
    const a = document.createElement("a");
    a.href = url;
    a.download = `${formData.reportName}.pdf`; // Set the file name
    a.click();

    // Clean up
    URL.revokeObjectURL(url);
  } else {
    message.error("文件不存在！");
  }
  app?.proxy?.loading.hide();
};

const cancelExportRecord = () => {
  openExportRecord.value = false;
};

watch(
  () => configData.value.eegFilter.eeg_channel_count,
  (value) => {
    if (value == 4) {
      eegChannel.value = eegFourChannelName;
    } else if (value == 2) {
      eegChannel.value = eegChannelName;
    }
  },
  {
    immediate: true,
  }
);

watch(isConnect, (newValue) => {
  if (!newValue) {
    worker?.terminate();
  } else {
    initArr();
  }
});

const handleStartRecordModal = (e?: MouseEvent) => {
  ipcRenderer.send("close-store");
  configData.value.hrv.isRecord = false;
  validate().then(() => {
    message.success("开始记录！");
    configData.value.irFilter.age = parseInt(formData.age + "");
    configData.value.hrv.step_time = configData.value.hrv.analysis_step_time;
    configData.value.hrv.isAnalysis = !configData.value.hrv.isAnalysis;
    ipcRenderer.send(
      "change-config-field",
      JSON.stringify({
        field: "hrv-start-analysis",
        config: configData.value,
      })
    );
    openStartRecordModal.value = false;
    workerInterval?.terminate();
    workerInterval = new Worker(
      new URL("../../../worker/setInterval.js", import.meta.url)
    );
    workerInterval.onmessage = restInterval;
    // 设置接收到/timerWorker.js消息时的执行函数;
    // 发送start类型消息
    app?.proxy?.loading.show(`Start resting ${restSeconds.value}s`);
    workerInterval.postMessage({ type: "start", time: 1 * 1000 });
  });
};

// 静息
const restInterval = (e) => {
  app?.proxy?.loading.show(`Start resting ${restSeconds.value - e.data}s`);
  if (restSeconds.value - e.data == 0) {
    startRecordData();
  }
};

// 开始录制
const startRecordData = () => {
  app?.proxy?.loading.hide();
  workerInterval?.terminate();
  configData.value.hrv.isRecord = true;
  videoRef.value.play();
  let instanceID = generateUniqueId();
  formData.recoredCreateTime = new Date().getTime();

  db.insert("reportRecord", {
    instanceID: instanceID,
    ...toRaw(formData),
    waveLength: configData.value.irFilter.is2wave
      ? 2
      : configData.value.irFilter.is3wave
      ? 3
      : 4,
    recoredCreateTime: formData.recoredCreateTime,
    configText: JSON.stringify({
      ir_sample_rate: configData.value.irFilter.ir_sample_rate,
      motion: configData.value.motion,
      eeg_sample_rate: configData.value.eegFilter.sample_rate,
      eeg_channel_count: configData.value.eegFilter.eeg_channel_count,
    }),
  }).then((res) => {
    tempRecordId = res;
    recordLastID.value = instanceID;
    // 创建存储进程
    ipcRenderer.send("create-store");
    // 开始存储
    ipcRenderer.send("start-store", {
      recordLastID: tempRecordId, // 这个是recordId
      field: "reportSource",
    });
  });

  worker?.terminate();
  // 开始计时
  worker = new Worker(
    new URL("../../../worker/setTimeout.js", import.meta.url)
  );
  // 设置接收到/timerWorker.js消息时的执行函数;
  worker.onmessage = stopRecord;
  // 发送start类型消息
  worker.postMessage({ type: "start", time: numSeconds.value * 1000 });
};

const stopRecord = () => {
  worker?.terminate();
  reportRecordSucess();
};

const clearRecord = () => {
  configData.value.hrv.isRecord = false;
  configData.value.hrv.isAnalysis = false;
  videoRef.value.pause();
  videoRef.value.currentTime = 0;
  ipcRenderer.send("stop-store", {
    field: "reportSource",
  });
  ipcRenderer.send(
    "change-config-field",
    JSON.stringify({
      field: "hrv-start-analysis",
      config: configData.value,
    })
  );
};
// 计时结束,报告记录完成
const reportRecordSucess = () => {
  clearRecord();
  app?.proxy?.loading.show("生成中...");
  timer && clearTimeout(timer);
  timer = setTimeout(() => {
    message.error("生成失败");
    app?.proxy?.loading.hide();
  }, 1000 * 60);
  let recoredEndTime = new Date().getTime();
  db.update(
    "reportRecord",
    {
      recoredEndTime: recoredEndTime,
      recoredTotalTime: recoredEndTime - formData.recoredCreateTime,
    },
    { id: tempRecordId }
  ).then(() => {
    setTimeout(() => {
      openExportRecord.value = true;
      nextTick(() => {
        calculate();
      });
    }, 3000);
    // exportPdf();
  });
};

const initArr = () => {
  formData.age = configData.value.irFilter.default_age;
  formData.gender = 1;
  formData.name = "";
  formData.reportName = "";
  eegPkgDataList = [];
  irPkgDataList = [];
  mindRestList = [];
  heartRateList = [];
  timeIndicatorTable.value = [];
  frequencyIndicatorTable.value = [];
  frequencyIndicatorBandTable.value = [];
  hrvResult.timeIndicatorValue1 = [];
  hrvResult.timeIndicatorValue2 = [];
  hrvResult.frequencyIndicatorValue1 = [];
  hrvResult.frequencyIndicatorValue2 = [];
  resetFields();
};

const generateUniqueId = () => {
  // 获取当前时间戳的毫秒部分
  const timestamp = Date.now().toString(36).slice(-8); // 转换为36进制并取后8位

  // 生成一个随机的6位36进制数
  const randomPart = Math.random().toString(36).slice(2, 8);

  // 合并时间戳和随机部分，总共12位
  const uniqueId = timestamp + randomPart;

  return uniqueId;
};

const handleEndRecordModal = () => {
  openStartRecordModal.value = false;
};

// 时域指标表格
interface indicatorTableType {
  key: number;
  parame1: string;
  value1: string;
  unit1: string;
  parame2: string;
  value2: string;
  unit2: string;
}
const timeIndicatorTable = ref<indicatorTableType[]>([]);
const timeIndicatorTableSharedOnCell = (_, index) => {
  if (index === 0 || index === 6 || index === 9) {
    return {
      colSpan: 0,
    };
  }
};
const timeIndicatorTableColumns = [
  {
    title: "参数",
    dataIndex: "parame1",
    customCell: (_, index) => {
      if (index === 0 || index === 6 || index === 9) {
        return {
          colSpan: 6,
        };
      } else {
        return {
          colSpan: 1,
        };
      }
    },
  },
  {
    title: "数值",
    dataIndex: "value1",
    customCell: timeIndicatorTableSharedOnCell,
  },
  {
    title: "单位",
    dataIndex: "unit1",
    customCell: timeIndicatorTableSharedOnCell,
  },
  {
    title: "参数",
    dataIndex: "parame2",
    customCell: timeIndicatorTableSharedOnCell,
  },
  {
    title: "数值",
    dataIndex: "value2",
    customCell: timeIndicatorTableSharedOnCell,
  },
  {
    title: "单位",
    dataIndex: "unit2",
    customCell: timeIndicatorTableSharedOnCell,
  },
];

// 频域指标表格
const frequencyIndicatorTable = ref<indicatorTableType[]>([]);
const frequencyIndicatorTableSharedOnCell = (_, index) => {
  if (index === 0 || index === 3 || index === 7 || index === 10) {
    return {
      colSpan: 0,
    };
  }
};
const frequencyIndicatorTableColumns = [
  {
    title: "参数",
    dataIndex: "parame1",
    customCell: (_, index) => {
      if (index === 0 || index === 3 || index === 7 || index === 10) {
        return {
          colSpan: 6,
        };
      } else {
        return {
          colSpan: 1,
        };
      }
    },
  },
  {
    title: "数值",
    dataIndex: "value1",
    customCell: frequencyIndicatorTableSharedOnCell,
  },
  {
    title: "单位",
    dataIndex: "unit1",
    customCell: frequencyIndicatorTableSharedOnCell,
  },
  {
    title: "参数",
    dataIndex: "parame2",
    customCell: frequencyIndicatorTableSharedOnCell,
  },
  {
    title: "数值",
    dataIndex: "value2",
    customCell: frequencyIndicatorTableSharedOnCell,
  },
  {
    title: "单位",
    dataIndex: "unit2",
    customCell: frequencyIndicatorTableSharedOnCell,
  },
];

//频域指标频带划分
const frequencyIndicatorBandTable = ref<indicatorTableType[]>([]);
const frequencyIndicatorBandTableSharedOnCell = (_, index) => {
  if (index === 0) {
    return {
      colSpan: 0,
    };
  }
};
const frequencyIndicatorBandTableColumns = [
  {
    title: "参数",
    dataIndex: "parame1",
    customCell: (_, index) => {
      if (index === 0) {
        return {
          colSpan: 6,
        };
      } else {
        return {
          colSpan: 1,
        };
      }
    },
  },
  {
    title: "数值",
    dataIndex: "value1",
    customCell: frequencyIndicatorBandTableSharedOnCell,
  },
  {
    title: "单位",
    dataIndex: "unit1",
    customCell: frequencyIndicatorBandTableSharedOnCell,
  },
  {
    title: "参数",
    dataIndex: "parame2",
    customCell: frequencyIndicatorBandTableSharedOnCell,
  },
  {
    title: "数值",
    dataIndex: "value2",
    customCell: frequencyIndicatorBandTableSharedOnCell,
  },
  {
    title: "单位",
    dataIndex: "unit2",
    customCell: frequencyIndicatorBandTableSharedOnCell,
  },
];

// 初始化频域指标频带划分
const initFrequencyIndicatorBandTable = () => {
  let parameList1 = ["频带划分", "ULF", "LF"];
  let unitList = ["", "Hz", "Hz"];
  let valueList1 = ["", "0-0.0033", "0.040-0.150"];
  let parameList2 = ["", "VLF", "HF"];
  let valueList2 = ["", "0.000-0.040", "0.150-0.400"];

  for (let i = 0; i < parameList1.length; i++) {
    frequencyIndicatorBandTable.value.push({
      key: i,
      parame1: parameList1[i],
      unit1: unitList[i],
      value1: valueList1[i],
      parame2: parameList2[i],
      unit2: unitList[i],
      value2: valueList2[i],
    });
  }
};

// 初始化时域指标
const initTimeIndicatorTable = () => {
  let parameList1 = [
    "NNI参数",
    "RR间隔数量（NNI）",
    "平均RR间隔时间（NNI_Average）",
    "RR间隔最小值(NNI_Min)",
    "RR间隔最大值(NNI_Max)",
    "SDNN",
    "NNI间隔参数",
    "相邻RR间隔平均值(Delta_NNI)",
    "相邻RR间隔最大值(Delta_NNI_Max)",
    "心率HR参数",
    "心率平均值(HR_Average)",
    "心率最大值(HR_Max)",
  ];
  let unitList1 = [
    "",
    "-",
    "ms",
    "ms",
    "ms",
    "ms",
    "",
    "ms",
    "ms",
    "",
    "bpm",
    "bpm",
  ];
  let parameList2 = [
    "",
    "RMSSD",
    "NN20",
    "NN50",
    "PNN20",
    "PNN50",
    "",
    "相邻RR间隔最小值(Delta_NNI_Min)",
    "相邻RR间隔标准差(SD_Delta_NNI)",
    "",
    "心率最小值(HR_Min)",
    "心率标准差(SD_HR)",
  ];
  let unitList2 = [
    "",
    "ms",
    "-",
    "-",
    "-",
    "-",
    "",
    "ms",
    "ms",
    "",
    "bpm",
    "bpm",
  ];

  for (let i = 0; i < parameList1.length; i++) {
    timeIndicatorTable.value.push({
      key: i,
      parame1: parameList1[i],
      unit1: unitList1[i],
      value1: hrvResult.timeIndicatorValue1[i],
      parame2: parameList2[i],
      unit2: unitList2[i],
      value2: hrvResult.timeIndicatorValue2[i],
    });
  }
};

// 初始化频域指标
const initFrequencyIndicatorTable = () => {
  let parameList1 = [
    "功率最大频率点",
    "ULF",
    "LF",
    "绝对功率参数",
    "ULF",
    "LF",
    "TP",
    "相对功率参数",
    "ULF",
    "LF",
    "对数功率参数",
    "ULF",
    "LF",
  ];
  let unitList1 = [
    "",
    "Hz",
    "Hz",
    "",
    "ms^2",
    "ms^2",
    "ms^2",
    "",
    "%",
    "%",
    "",
    "-",
    "-",
  ];
  let parameList2 = [
    "",
    "VLF",
    "HF",
    "",
    "VLF",
    "HE",
    "LF/HF Ration",
    "",
    "VLF",
    "HF",
    "",
    "VLF",
    "HF",
  ];
  let unitList2 = [
    "",
    "Hz",
    "Hz",
    "",
    "ms^2",
    "ms^2",
    "-",
    "",
    "%",
    "%",
    "",
    "-",
    "-",
  ];

  for (let i = 0; i < parameList1.length; i++) {
    frequencyIndicatorTable.value.push({
      key: i,
      parame1: parameList1[i],
      unit1: unitList1[i],
      value1: hrvResult.frequencyIndicatorValue1[i],
      parame2: parameList2[i],
      unit2: unitList2[i],
      value2: hrvResult.frequencyIndicatorValue2[i],
    });
  }
};

const changeMode = () => {
  if (stateValue.value === "Concentration") {
    videoSrc.value = concentrationVideo;
  } else if (stateValue.value === "Relaxation") {
    videoSrc.value = relaxationVideo;
  }
};

const changeStatus = () => {
  if (!isConnect.value && !configData.value.hrv.isAnalysis) {
    return message.error("请先连接蓝牙");
  }
  if (!configData.value.hrv.isAnalysis) {
    initArr();
    openStartRecordModal.value = true;
  } else {
    Modal.confirm({
      title: "Tip",
      content: `Do you want to end this test?`,
      okText: "Confirm",
      cancelText: "Cancel",
      onOk() {
        worker?.terminate();
        clearRecord();
        initArr();
        db.delete("reportRecord", { id: tempRecordId });
        db.delete("reportSource", { recordId: tempRecordId });
      },
    });
  }

  // exportPdf();
};

onMounted(() => {
  ipcRenderer.on("get-calculate-data-success", getCalculateDataSuccess);
  ipcRenderer.on("calculate-hrv-result", calculateHrvResult);
  ipcRenderer.on("calculate-fft-ps-result", calculateFFTPsResult);
  ipcRenderer.on("pdf-read-response", pdfReadResponse);
  // 时域波形-滤波器做0.5-45Hz的处理后展示
  configData.value.irFilter.plotType = 3;
  configData.value.eegFilter.fl = 0.5;
  configData.value.eegFilter.fh = 45;
  ipcRenderer.send(
    "change-config-field",
    JSON.stringify({
      field: "analysis-config",
      config: configData.value,
    })
  );

  initialize();
});

// 初始化
const initialize = () => {
  eegPkgDataList = [];
  irPkgDataList = [];
  mindRestList = [];
  heartRateList = [];
  // 正常模式
  bluetooth.addNotice(bluetoothNotice);
};

// 蓝牙数据通知
const bluetoothNotice = (data) => {
  if (configData.value.hrv.isAnalysis && configData.value.hrv.isRecord) {
    delete data.psd_relative_percent_s;
    delete data.ir_od_date;
    delete data.loss_data_info_el;
    delete data.baseline_ok;
    delete data.psd_s_multiple;
    delete data.psd_s;
    delete data.psd_relative_s;
    delete data.acceleration_x;
    delete data.acceleration_y;
    delete data.acceleration_z;
    delete data.temperature;
    delete data.error_state;
    handlePkgList(data);
  }
};

// 数据包处理
const handlePkgList = (data) => {
  if (data.pkg_type === 1) {
    if (eegPkgDataList.length) {
      let gapTime =
        eegPkgDataList[eegPkgDataList.length - 1].time_mark -
        eegPkgDataList[0].time_mark;
      if (gapTime <= numSeconds.value * 1000) {
        mindRestList.push([
          gapTime,
          (data.mindfulness_restfulness_s[0][parseState(stateValue.value)] +
            data.mindfulness_restfulness_s[1][parseState(stateValue.value)]) /
            2,
        ]);
        undatelineChart(
          lineChart1,
          concentrationChannel.value[0],
          mindRestList
        );
        eegPkgDataList.push(data);
      }
    } else {
      eegPkgDataList.push(data);
    }
    // undateEegTimeSerie();
  } else if (data.pkg_type === 2) {
    // console.log('data.heart_rate',data.heart_rate);

    if (irPkgDataList.length) {
      let gapTime =
        irPkgDataList[irPkgDataList.length - 1].time_mark -
        irPkgDataList[0].time_mark;
      if (gapTime <= numSeconds.value * 1000) {
        heartRateList.push([gapTime, data.heart_rate]);
        undatelineChart(lineChart2, heartRateChannel.value[0], heartRateList);
        irPkgDataList.push(data);
      }
    } else {
      irPkgDataList.push(data);
    }
  }
};

const calculateHrvResult = (event, data) => {
  let NNI = data.timedomain_NNI;
  // NNI 参数、平均RR间隔时间(NNI_Average)、RR间隔最小值(NNI_Min)、RR间隔最大值(NNI_Max)、SDNN
  // RMSSD、NN20、NN50、pNN20、pNN50
  let DIFF = data.timedomain_NNI_diff;
  //NNI差值参数：相邻RR间隔平均值、相邻RR间隔最小值、相邻RR间隔最大值、相邻RR间隔标准差
  let HR = data.timedomain_HR;
  //HR参数：HR平均值、HR最小值、HR最大值、HR标准差
  hrvResult.timeIndicatorValue1 = [
    "",
    data.number_of_hr[0], // RR间隔数量（NNI）
    NNI[0],
    NNI[1],
    NNI[2],
    NNI[3],
    "",
    DIFF[0],
    DIFF[2],
    "",
    HR[0],
    HR[2],
  ];
  hrvResult.timeIndicatorValue2 = [
    "",
    NNI[4],
    NNI[5],
    NNI[6],
    NNI[7],
    NNI[8],
    "",
    DIFF[1],
    DIFF[3],
    "",
    HR[1],
    HR[3],
  ];
  initTimeIndicatorTable();
  //-------------------------------------------------------------------
  let max_f = data.hrv_fdomain_max_f; //功率最大频率点：ULF、VLF、LF、HF
  let spec = data.hrv_fdomain_spec; //绝对ULF、VLF、LF、HF，TP、LF/HF，
  let percent = data.hrv_fdomain_percent; //相对功率：ULF、VLF、LF、HF
  let spec_db = data.hrv_fdomain_spec_db; //对数功率：ULF、VLF、LF、HF、TP

  hrvResult.frequencyIndicatorValue1 = [
    "",
    max_f[0],
    max_f[2],
    "",
    spec[0],
    spec[2],
    spec[4],
    "",
    percent[0],
    percent[2],
    "",
    spec_db[0],
    spec_db[2],
  ];
  hrvResult.frequencyIndicatorValue2 = [
    "",
    max_f[1],
    max_f[3],
    "",
    spec[1],
    spec[3],
    spec[5],
    "",
    percent[1],
    percent[3],
    "",
    spec_db[1],
    spec_db[3],
  ];
  initFrequencyIndicatorTable();
  //-------------------------------------------------------------------
  initFrequencyIndicatorBandTable();
  // setTimeout(() => {
  //   ipcRenderer.send("calculate-fft-ps");
  // },500)
  timer && clearTimeout(timer);
  drawChart();
};

const calculateFFTPsResult = (event, data) => {
  fftpsResult = data;
  // ipcRenderer.send("close-calculate");
  timer && clearTimeout(timer);
  drawChart();
};

const undatelineChart = (refName, name, data, isDraw = false) => {
  refName.value.setOption({
    series: [
      {
        name,
        data: data,
      },
    ],
  });
  isDraw && refName.value.drawChart();
};

// 更新eeg时域波形数据
const undateEegTimeSerie = () => {
  eegChannel.value.forEach((item, index) => {
    eegTimeRefs.value[index].setOption({
      series: [
        {
          name: item,
          data: conversionPkgtoSeriesData(item, numSeconds.value),
        },
      ],
    });

    eegTimeRefs.value[index].drawChart();
  });
};

// 更新eeg节律波形数据
const undateEegBandsTime = () => {
  eegChannel.value.forEach((item, index) => {
    eegBandsTimeRefs.value[index].setOption({
      series: [
        {
          name: "EEG",
          data: conversionPkgtoSeriesData(item, numSeconds.value),
        },
        {
          name: "DELTA",
          data: conversionPkgtoBarnsTimeOrRelated(
            "time_e_s",
            item,
            0,
            numSeconds.value
          ),
        },
        {
          name: "THETA",
          data: conversionPkgtoBarnsTimeOrRelated(
            "time_e_s",
            item,
            1,
            numSeconds.value
          ),
        },
        {
          name: "ALPHA",
          data: conversionPkgtoBarnsTimeOrRelated(
            "time_e_s",
            item,
            2,
            numSeconds.value
          ),
        },
        {
          name: "BETA",
          data: conversionPkgtoBarnsTimeOrRelated(
            "time_e_s",
            item,
            3,
            numSeconds.value
          ),
        },
        {
          name: "GAMMA",
          data: conversionPkgtoBarnsTimeOrRelated(
            "time_e_s",
            item,
            4,
            numSeconds.value
          ),
        },
      ],
    });
    eegBandsTimeRefs.value[index].drawChart();
  });
};

// 更新eeg脑电功率谱数据
const undateEegPsd = () => {
  eegPsdFp1Chart.value.setOption({
    series: [
      {
        name: eegPsdFp1Channel.value[0],
        data: conversionPkgtoPsd(eegPsdFp1Channel.value[0]),
      },
    ],
  });
  eegPsdFp2Chart.value.setOption({
    series: [
      {
        name: eegPsdFp2Channel.value[0],
        data: conversionPkgtoPsd(eegPsdFp2Channel.value[0]),
      },
    ],
  });
  eegPsdFp1Chart.value.drawChart();
  eegPsdFp2Chart.value.drawChart();
};
// 更新eeg脑电节律能量数据
const undateRelatedPower = () => {
  eegChannel.value.forEach((item, index) => {
    eegRelatedPowerRefs.value[index].setOption({
      series: [
        [
          {
            name: "γ wave",
            data: conversionPkgtoBarnsTimeOrRelated(
              "psd_relative_s",
              item,
              4,
              numSeconds.value
            ),
          },
          {
            name: "β wave",
            data: conversionPkgtoBarnsTimeOrRelated(
              "psd_relative_s",
              item,
              3,
              numSeconds.value
            ),
          },
          {
            name: "α wave",
            data: conversionPkgtoBarnsTimeOrRelated(
              "psd_relative_s",
              item,
              2,
              numSeconds.value
            ),
          },
          {
            name: "θ wave",
            data: conversionPkgtoBarnsTimeOrRelated(
              "psd_relative_s",
              item,
              1,
              numSeconds.value
            ),
          },
          {
            name: "δ wave",
            data: conversionPkgtoBarnsTimeOrRelated(
              "psd_relative_s",
              item,
              0,
              numSeconds.value
            ),
          },
        ],
      ],
    });

    eegRelatedPowerRefs.value[index].drawChart();
  });
};
// 更新ir近红外时域指标数据
const undateIrTimeSerie = () => {
  let groupSeriesData: any = [];

  for (let i = 1; i <= 8; i++) {
    let seriesName;
    if (i >= 5) {
      seriesName = `S2D${i}`;
    } else {
      seriesName = `S1D${i}`;
    }
    const radioRows: any = [];

    for (let j = 0; j < 3; j++) {
      const suffix = ["O", "R", "T"][j];
      radioRows.push({
        chanIndex: i,
        name: `Hb${suffix}`,
        radioIndex: j + 1,
      });
    }

    groupSeriesData.push({
      name: seriesName,
      radioRows,
    });
  }

  irTimeSerie.value.setOption({
    series: groupSeriesData.map((groupItem, index) => {
      return groupItem.radioRows.map((item) => {
        return conversionPkgtoTimeSeries(
          "concentration_date",
          mapChanToField(item.chanIndex),
          mapRadioToField(item.radioIndex),
          item.name,
          numSeconds.value
        );
      });
    }),
  });
  irTimeSerie.value.drawChart();
};

const mapRadioToField = (index) => {
  switch (
    index // 波形索引
  ) {
    case 1:
      return 0;
    case 2:
      return 1;
    case 3:
      return 2;
    default:
      return 0;
  }
};

const mapChanToField = (index) => {
  return index - 1;
};

const conversionPkgtoTimeSeries = (field, channel, index, name, step) => {
  if (irPkgDataList.length < 1) return [];
  let maxTimer = irPkgDataList[irPkgDataList.length - 1].time_mark;
  let minTime = maxTimer - step * 1000;
  let sliceData = irPkgDataList.filter(
    (item) => item.time_mark >= minTime && item.time_mark <= maxTimer
  );
  let baseTime = 0;
  return {
    name: name,
    data: sliceData.map((item, sliceIndex) => {
      if (sliceIndex !== 0) {
        baseTime += item.time_mark - sliceData[sliceIndex - 1].time_mark;
      }
      return [baseTime, item[field][channel][index]];
    }),
  };
};

const conversionPkgtoPsd = (typeChannel) => {
  if (eegPkgDataList.length < 1) return 0;
  return fftpsResult[parseChannel(typeChannel)].map((item, index) => {
    return [(index * 1000) / 728.2, item]; //* 1000 / 2.056
  });
};

const conversionPkgtoSeriesData = (typeChannel, step) => {
  if (eegPkgDataList.length < 1) return [];
  let maxTimer = eegPkgDataList[eegPkgDataList.length - 1].time_mark;
  let minTime = maxTimer - step * 1000;
  let sliceData = eegPkgDataList.filter(
    (item) => item.time_mark >= minTime && item.time_mark <= maxTimer
  );
  let baseTime = 0;

  let tempSliceData: any = [];
  for (let sliceIndex = 0; sliceIndex < sliceData.length; sliceIndex++) {
    const item = sliceData[sliceIndex];
    if (sliceIndex !== 0) {
      baseTime += item.time_mark - sliceData[sliceIndex - 1].time_mark;
    }
    let brain_elec_channel = item.brain_elec_channel[parseChannel(typeChannel)];
    if (item.isLosspkg && sliceIndex !== sliceData.length - 1) {
      // 补包的情况下自己算间隔，因为丢包后的时间戳不太对
      let TimeGap =
        (sliceData[sliceIndex + 1].time_mark - item.time_mark) /
        item.eeg_data_num;
      for (let brainIndex = 0; brainIndex < item.eeg_data_num; brainIndex++) {
        tempSliceData.push([
          baseTime + brainIndex * TimeGap,
          brain_elec_channel[brainIndex],
          item.time_stamp + brainIndex * TimeGap,
        ]);
      }
    } else {
      for (let brainIndex = 0; brainIndex < item.eeg_data_num; brainIndex++) {
        tempSliceData.push([
          baseTime + brainIndex * EEGTimeGap,
          brain_elec_channel[brainIndex],
          item.time_stamp + brainIndex * EEGTimeGap,
        ]);
      }
    }
  }
  return tempSliceData;
};

const conversionPkgtoBarnsTimeOrRelated = (field, typeChannel, index, step) => {
  if (eegPkgDataList.length < 1) return [];
  let maxTimer = eegPkgDataList[eegPkgDataList.length - 1].time_mark;
  let minTime = maxTimer - step * 1000;
  let sliceData = eegPkgDataList.filter(
    (item) => item.time_mark >= minTime && item.time_mark <= maxTimer
  );
  let baseTime = 0;
  let tempSliceData: any = [];
  for (let sliceIndex = 0; sliceIndex < sliceData.length; sliceIndex++) {
    const item = sliceData[sliceIndex];
    if (sliceIndex !== 0) {
      baseTime += item.time_mark - sliceData[sliceIndex - 1].time_mark;
    }
    let fieldDataList = item[field + "_multiple"];
    if (item.isLosspkg && sliceIndex !== sliceData.length - 1) {
      // 补包的情况下自己算间隔，因为丢包后的时间戳不太对
      let TimeGap =
        (sliceData[sliceIndex + 1].time_mark - item.time_mark) /
        item.eeg_data_num;
      for (
        let fieldIndex = 0;
        fieldIndex < fieldDataList.length;
        fieldIndex++
      ) {
        tempSliceData.push([
          baseTime + fieldIndex * TimeGap,
          fieldDataList[fieldIndex][parseChannel(typeChannel)][index],
        ]);
      }
    } else {
      for (
        let fieldIndex = 0;
        fieldIndex < fieldDataList.length;
        fieldIndex++
      ) {
        tempSliceData.push([
          baseTime + fieldIndex * EEGTimeGap,
          fieldDataList[fieldIndex][parseChannel(typeChannel)][index],
        ]);
      }
    }
  }
  return tempSliceData;
};

const parseChannel = (channel: string) => {
  switch (channel) {
    case "Fp1":
      return 1;
    case "Fp2":
      return 0;
    case "Fpz":
      return 3;
    case "AF7":
      return 1;
    case "AF8":
      return 0;
    case "M2":
      return 2;
  }
  return 0;
};

const parseState = (state: string) => {
  switch (state) {
    case "Concentration":
      return 0;
    case "Relaxation":
      return 1;
  }
  return 0;
};

// 绘制图形
const drawChart = () => {
  undateEegTimeSerie();
  undateEegBandsTime();
  // undateEegPsd();
  undateRelatedPower();
  undatelineChart(
    mindRestChart,
    concentrationChannel.value[0],
    mindRestList,
    true
  );
  undateIrTimeSerie();
  undatelineChart(
    HeartRateChart,
    heartRateChannel.value[0],
    heartRateList,
    true
  );
  nextTick(() => {
    exportPdf();
  });
  // ipcRenderer.send("create-child");
  // configData.value.isCloseChild = false
};

// 计算hrv
const calculate = () => {
  configData.value.hrv.isAnalysis = false;
  ipcRenderer.send(
    "change-config-field",
    JSON.stringify({
      field: "hrv-end-analysis",
      config: configData.value,
    })
  );
  ipcRenderer.send("calculate-hrv");
  // ipcRenderer.send("get-calculate-data");
};

const getCalculateDataSuccess = () => {
  setTimeout(() => {
    configData.value.isCloseChild = true;
    ipcRenderer.send("close-child");
    setTimeout(() => {
      ipcRenderer.send("create-calculate");
      ipcRenderer.send("calculate-hrv");
    }, 1000);
  }, 1000);
};

const exportPdf = () => {
  downloadPDF(pdf.value);
};

const downloadPDF = (page) => {
  html2canvas(page, {
    useCORS: true, //允许canvas画布内 可以跨域请求外部链接图片, 允许跨域请求。
    allowTaint: true, //允许跨域
    scale: 3, //设置放大倍数
    backgroundColor: "#ffffff", //背景色
  }).then((canvas) => {
    canvas2PDF(canvas);
  });
};

const canvas2PDF = (canvas) => {
  // 新建JsPDF对象
  const PDF = new jsPDF({
    orientation: "p", //参数： l：横向  p：纵向
    unit: "mm", //参数：测量单位（"pt"，"mm", "cm", "m", "in" or "px"）
    format: "a4", //A4纸
  });
  const ctx = canvas.getContext("2d");
  const a4w = 190;
  const a4h = 277; //A4大小，210mm x 297mm，四边各保留10mm *1.5的边距，显示区域190x277
  const imgHeight = Math.floor((a4h * canvas.width) / a4w); //按A4显示比例换算一页图像的像素高度
  let renderedHeight = 0;

  while (renderedHeight < canvas.height) {
    let page = document.createElement("canvas");
    page.width = canvas.width;
    page.height = Math.min(imgHeight, canvas.height - renderedHeight); //可能内容不足一页

    //用getImageData剪裁指定区域，并画到前面创建的canvas对象中
    page
      .getContext("2d")
      ?.putImageData(
        ctx.getImageData(
          0,
          renderedHeight,
          canvas.width,
          Math.min(imgHeight, canvas.height - renderedHeight)
        ),
        0,
        0
      );

    // canvas转图片数据保留10mm边距
    PDF.addImage(
      page.toDataURL("image/jpeg", 0.2),
      "JPEG",
      10,
      10,
      a4w,
      Math.min(a4h, (a4w * page.height) / page.width)
    );

    renderedHeight += imgHeight;

    //判断是否分页，如果后面还有内容，添加一个空页
    if (renderedHeight < canvas.height) {
      PDF.addPage();
    }
  }

  ipcRenderer.send("pdf-save", {
    fileName: recordLastID.value,
    pdfData: PDF.output("arraybuffer"),
  });

  // PDF.save("导出.pdf");
  app?.proxy?.loading.hide();
};

onBeforeUnmount(() => {
  //  关闭存储进程
  ipcRenderer.send("stop-store", {
    field: "source",
  });
  ipcRenderer.send("close-calculate");
  ipcRenderer.send("close-store");

  // 时域波形-滤波器做0.5-45Hz的处理后展示
  configData.value.eegFilter.fl = 0.1;
  configData.value.eegFilter.fh = 100;
  configData.value.irFilter.age = configData.value.irFilter.default_age;
  configData.value.hrv.step_time = configData.value.hrv.default_step_time;
  configData.value.irFilter.plotType = 1;
  ipcRenderer.send(
    "change-config-field",
    JSON.stringify({
      field: "analysis-config",
      config: configData.value,
    })
  );

  ipcRenderer.removeListener(
    "get-calculate-data-success",
    getCalculateDataSuccess
  );
  timer && clearTimeout(timer);
  ipcRenderer.removeListener("calculate-hrv-result", calculateHrvResult);
  ipcRenderer.removeListener("calculate-fft-ps-result", calculateFFTPsResult);
  ipcRenderer.removeListener("pdf-read-response", pdfReadResponse);
  bluetooth.removeNotice(bluetoothNotice);
  eegPkgDataList = null;
  irPkgDataList = null;
  mindRestList = null;
  heartRateList = null;
  bluetooth = null;
  db = null;
  timer = null;
  worker?.terminate();
});
</script>
<style scoped></style>
